package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.JTextField;

import MessagePacks.getCommitteePack;
import MessagePacks.getEstimationReport;
import MessagePacks.memApproveRepRead;
import entities.EstimationReport;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class EstimationReportRead extends MasterGUI {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton btnConfirmReading;
	private MainGUI mainGui;
	private int processID;
	private EstimationReport EsReport;
	private getEstimationReport getEsRpck;
	private  getCommitteePack gCommPck;
	private memApproveRepRead memAppRepPck;
	private JPanel panel;
	private String chairmanID;
	private String comitMem1ID;
	private String comitMem2ID;
	

	public EstimationReportRead(MainGUI mainGui) {
		super(mainGui);
		this.mainGui=mainGui;
		//System.out.println(MainGUI.process.getprocessID());
		this.processID=mainGui.process.getprocessID();
		System.out.println(this.processID);
		EsReport = new EstimationReport(processID);
		//System.out.println("EsReport Ok");
		getEsRpck = new getEstimationReport(EsReport);
		//System.out.println("EsReportPack Ok");
		mainGui.client.handleMessageFromClientUI(getEsRpck);
		//System.out.println("SendToClient Ok");
		getEsRpck = ((getEstimationReport)(mainGui.client.getMessage()));
		//System.out.println("getMessageOk");
		EsReport = getEsRpck.getReport();
		System.out.println(EsReport.getReportId()+" "+EsReport.getSystem()+" "+EsReport.getChangeDesc()+" "+EsReport.getLimitataions()+" "+EsReport.getHazards()+" "+EsReport.getEstTime());
		
		//Achieving The Current Committee ids
		getCurrentCommitteeIds();
		
		//Creating Pack For Reading Approval
		if((mainGui.usr.GetUser())==Integer.parseInt(comitMem1ID))
		memAppRepPck = new memApproveRepRead(1,processID);
		else if((mainGui.usr.GetUser())==Integer.parseInt(comitMem2ID))
			memAppRepPck = new memApproveRepRead(2,processID);
		

		
		panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblEvaluationReport = new JLabel("Evaluation Report");
		lblEvaluationReport.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblEvaluationReport);
		
		setBtnConfirmReading();
		
		JLabel lblLocationOfChange = new JLabel("Location Of Change:");
		lblLocationOfChange.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLocationOfChange.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblLocationOfChange.setBounds(142, 86, 185, 23);
		getContentPane().add(lblLocationOfChange);
		
		JLabel lblResult = new JLabel("Result");
		lblResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblResult.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblResult.setBounds(127, 150, 185, 23);
		getContentPane().add(lblResult);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDescription.setBounds(127, 238, 185, 23);
		getContentPane().add(lblDescription);
		
		JLabel lblLimitations = new JLabel("Limitations");
		lblLimitations.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLimitations.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblLimitations.setBounds(142, 336, 185, 23);
		getContentPane().add(lblLimitations);
		
		JLabel lblRisks = new JLabel("Risks");
		lblRisks.setHorizontalAlignment(SwingConstants.RIGHT);
		lblRisks.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblRisks.setBounds(127, 407, 185, 23);
		getContentPane().add(lblRisks);
		
		JLabel lblExecutionTimeEstimation = new JLabel("Execution Time Estimation");
		lblExecutionTimeEstimation.setHorizontalAlignment(SwingConstants.RIGHT);
		lblExecutionTimeEstimation.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblExecutionTimeEstimation.setBounds(127, 490, 185, 23);
		getContentPane().add(lblExecutionTimeEstimation);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(350, 92, 204, 49);
		getContentPane().add(scrollPane);
		
		JTextArea textAreaLocation = new JTextArea(EsReport.getSystem());
		textAreaLocation.setEditable(false);
		scrollPane.setViewportView(textAreaLocation);
	
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(350, 244, 204, 75);
		getContentPane().add(scrollPane_1);
		
		JTextArea textAreaDesc = new JTextArea(EsReport.getChangeDesc());
		textAreaDesc.setEditable(false);
		scrollPane_1.setViewportView(textAreaDesc);
		
		JScrollPane scrollPane_0 = new JScrollPane();
		scrollPane_0.setBounds(350, 154, 204, 75);
		getContentPane().add(scrollPane_0);
		
		JTextArea textAreaRes = new JTextArea(EsReport.getResult());
		textAreaRes.setEditable(false);
		scrollPane_0.setViewportView(textAreaRes);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(350, 342, 204, 49);
		getContentPane().add(scrollPane_2);
		
		JTextArea textAreaLimit = new JTextArea(EsReport.getLimitataions());
		textAreaLimit.setEditable(false);
		scrollPane_2.setViewportView(textAreaLimit);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(350, 413, 204, 62);
		getContentPane().add(scrollPane_3);
		
		JTextArea textAreaRisks = new JTextArea(EsReport.getHazards());
		textAreaRisks.setEditable(false);
		scrollPane_3.setViewportView(textAreaRisks);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(350, 496, 204, 16);
		getContentPane().add(scrollPane_4);
		
		JTextArea textAreaTimeEst = new JTextArea(Integer.toString(EsReport.getEstTime()));
		textAreaTimeEst.setEditable(false);
		scrollPane_4.setViewportView(textAreaTimeEst);
		
		JLabel lblProcessIDTitle = new JLabel("Process ID:");
		lblProcessIDTitle.setHorizontalAlignment(SwingConstants.RIGHT);
		lblProcessIDTitle.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblProcessIDTitle.setBounds(238, 51, 89, 24);
		getContentPane().add(lblProcessIDTitle);
		
		JLabel lblProcessIDNum = new JLabel(Integer.toString(this.processID));
		lblProcessIDNum.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblProcessIDNum.setBounds(350, 51, 131, 23);
		getContentPane().add(lblProcessIDNum);
		


	}

	public JButton getBtnConfirmReading() {
		return btnConfirmReading;
	}

	public void setBtnConfirmReading() {
		btnConfirmReading = new JButton("Confirm Reading");
		btnConfirmReading.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Sending Pack To HandleMessageFromClientUI
				mainGui.client.handleMessageFromClientUI(memAppRepPck);
				btnConfirmReading.setEnabled(false);
			}
		});
		btnConfirmReading.setBounds(391, 540, 126, 23);
		getContentPane().add(btnConfirmReading);
		
	}


	/**
	 * Method Gets The Current Committee Members Ids From DB And Set Them Is Class Variables
	 */
	private void getCurrentCommitteeIds(){
		/*Request For And Get The Current Committee From DataBase*/
		gCommPck = new getCommitteePack();
		mainGui.client.handleMessageFromClientUI(gCommPck);
		HashMap<String,String> hMap = ((getCommitteePack)(mainGui.client.getMessage())).getMyHashMap();
		
		/*Set Committee Members Id's In Class*/
		Set mapSet = (Set) hMap.entrySet();
		Iterator mapIterator = mapSet.iterator();
		while(mapIterator.hasNext()){
			Map.Entry<String, String> mapEntry = (Map.Entry) mapIterator.next();
			switch((String)mapEntry.getValue()){
			case "Chairman":{
							chairmanID = new String((String) mapEntry.getKey());
							System.out.println("Chaiman - ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue());
							break;
							}
			case "Member1":{
							comitMem1ID = new String((String) mapEntry.getKey());
							System.out.println("Mem1 - ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue());
							break;
							}
			case "Member2":{
							comitMem2ID = new String((String) mapEntry.getKey());
							System.out.println("Mem2 - ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue());
							break;
							}	
				default:System.out.println("ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue()+" - Shouldn't Happen");
			}
		}
	}
	
	

}
